<?php
/**
 * 物流跟踪 - 快递鸟
 */
class ControllerExtensionLogisticKdniao extends Controller {
    const EXTENSION_GROUP = 'logistic';

    const EXTENSION_TYPE = 'kdniao';

    const SETTING_CODE = self::EXTENSION_GROUP . '_' . self::EXTENSION_TYPE;

    const EXTENSION_PATH = 'extension/'. self::EXTENSION_GROUP. '/'  . self::EXTENSION_TYPE;

    /**
     * 需要提交 / 显示的字段 后缀 需要检查的必填字段
     */
    const SETTING_KEY_NAMES = ['e_business_id', 'app_key', 'sort_order', 'status'];

    private $error = array();

	public function index() {
		$this->load->language(self::EXTENSION_PATH);

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('setting/setting');

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			$this->model_setting_setting->editSetting(self::SETTING_CODE, $this->request->post);

			$this->session->data['success'] = $this->language->get('text_success');

			$this->response->redirect($this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=' . self::EXTENSION_GROUP));
		}

        $data['breadcrumbs'] = $this->assembleBreadCrumbs();
        $data['action'] = $this->url->link(self::EXTENSION_PATH, 'user_token=' . $this->session->data['user_token']);
		$data['cancel'] = $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type='. self::EXTENSION_GROUP);

        $data = $this->appendDataErrors($data, ['warning'] + self::SETTING_KEY_NAMES);
        $data = $this->appendDataSettingKeys($data, self::SETTING_KEY_NAMES);

		$data['header'] = $this->load->controller('common/header');
		$data['column_left'] = $this->load->controller('common/column_left');
		$data['footer'] = $this->load->controller('common/footer');

		$this->response->setOutput($this->load->view(self::EXTENSION_PATH, $data));
	}

	private function validate() {
		if (!$this->user->hasPermission('modify', self::EXTENSION_PATH)) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		$this->addErrorIfPostKeyNamesNotExist(self::SETTING_KEY_NAMES);

		return ! $this->error;
	}

    /**
     * @param $name
     *
     * @return string
     */
    private function settingKey($name)
    {
        return self::SETTING_CODE . "_$name";
	}

    /**
     * @param $data
     *
     * @return mixed
     */
    protected function assembleBreadCrumbs()
    {
        return [
            [
                'text' => $this->language->get('text_home'),
                'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
            ],
            [
                'text' => $this->language->get('text_extension'),
                'href' => $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=' . self::EXTENSION_GROUP)
            ],
            [
                'text' => $this->language->get('heading_title'),
                'href' => $this->url->link(self::EXTENSION_PATH, 'user_token=' . $this->session->data['user_token'])
            ],
        ];
    }

    /**
     * @param $data
     * @param $name
     *
     * @return mixed
     */
    private function appendDataSettingKey($data, $name)
    {
        $key = $this->settingKey($name);
        if (isset($this->request->post[$key])) {
            $data[$key] = $this->request->post[$key];
        } else {
            $data[$key] = $this->config->get($key);
        }

        return $data;
    }

    /**
     * @param array $names
     */
    private function addErrorIfPostKeyNamesNotExist(array $names)
    {
        foreach ($names as $name)
        if (! $this->request->post[$this->settingKey($name)]) {
            $this->error[$name] = $this->language->get('error_' . $name);
        }
    }

    /**
     * @param array $data
     *
     * @param array $names
     *
     * @return array
     */
    private function appendDataErrors(array $data, array $names)
    {
        foreach ($names as $name)
        {
            if (isset($this->error[$name])) {
                $data['error_' . $name] = $this->error[$name];
            } else {
                $data['error_' . $name] = '';
            }
        }

        return $data;
    }

    /**
     * @param array $data
     * @param array $names
     *
     * @return array|mixed
     */
    private function appendDataSettingKeys(array $data, array $names)
    {
        foreach ($names as $name)
        {
            $data = $this->appendDataSettingKey($data, $name);
        }

        return $data;
    }
}